package com.javacoo.swing.core.utils;

import org.apache.http.*;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
 

public class HttpClientUtil {
 
	 static final int timeOut = 10000;
	    private static CloseableHttpClient httpClient = null;
	    private static IdleConnectionMonitorThread idleConnectionMonitor = null;
	    private final static Object syncLock = new Object();
	 
	    private  void config(HttpRequestBase httpRequestBase) {
	        // 设置Header等
	        // httpRequestBase.setHeader("User-Agent", "Mozilla/5.0");
	        // httpRequestBase
	        // .setHeader("Accept",
	        // "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
	        // httpRequestBase.setHeader("Accept-Language",
	        // "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");// "en-US,en;q=0.5");
	        // httpRequestBase.setHeader("Accept-Charset",
	        // "ISO-8859-1,utf-8,gbk,gb2312;q=0.7,*;q=0.7");
	 
	        // 配置请求的超时设置
	        RequestConfig requestConfig = RequestConfig.custom()
	                .setConnectionRequestTimeout(timeOut)
	                .setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();
	        httpRequestBase.setConfig(requestConfig);
	    }
	 
	    public static  CloseableHttpClient getHttpClient(String url) {
	        String hostname = url.split("/")[2];
	        int port = 80;
	        if (hostname.contains(":")) {
	            String[] arr = hostname.split(":");
	            hostname = arr[0];
	            port = Integer.parseInt(arr[1]);
	        }
	        if (httpClient == null) {
	            synchronized (syncLock) {
	                if (httpClient == null) {
	                    httpClient = createHttpClient(1024, 32, 64, hostname, port);
	                }
	            }
	        }
	        return httpClient;
	    }
	 
	    public static CloseableHttpClient createHttpClient(int maxTotal,int maxPerRoute, int maxRoute, String hostname, int port) {
	        
	        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
	        // 设置最大连接数
	        cm.setMaxTotal(maxTotal);
	        // 将每个路由默认最大连接数
	        cm.setDefaultMaxPerRoute(maxPerRoute);
	        HttpHost httpHost = new HttpHost(hostname, port);
	        // 设置目标主机对应的路由的最大连接数，会覆盖setDefaultMaxPerRoute设置的默认值
	        cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
	 
	        // 请求重试处理
	        HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
	            public boolean retryRequest(IOException exception,int executionCount, HttpContext context) {
	                if (executionCount >= 3) {// 如果已经重试了3次，就放弃
	                    return false;
	                }
	                if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接，那么就重试
	                    return true;
	                }
	                if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
	                    return false;
	                }
	                if (exception instanceof InterruptedIOException) {// 超时
	                    return false;
	                }
	                if (exception instanceof UnknownHostException) {// 目标服务器不可达
	                    return false;
	                }
	                if (exception instanceof ConnectTimeoutException) {// 连接被拒绝
	                    return false;
	                }
	                if (exception instanceof SSLException) {// SSL握手异常
	                    return false;
	                }
	 
	                HttpClientContext clientContext = HttpClientContext.adapt(context);
	                HttpRequest request = clientContext.getRequest();
	                // 如果请求是幂等的，就再次尝试
	                if (!(request instanceof HttpEntityEnclosingRequest)) {
	                    return true;
	                }
	                return false;
	            }
	        };
	 
	        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).build();
	        idleConnectionMonitor = new IdleConnectionMonitorThread(cm);
	        idleConnectionMonitor.start();
	 
	        return httpClient;
	    }
	 
	    private static void setPostParams(HttpPost httpost,Map<String, Object> params) {
	        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
	        Set<String> keySet = params.keySet();
	        for (String key : keySet) {
	            nvps.add(new BasicNameValuePair(key, params.get(key).toString()));
	        }
	        try {
	            httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
	        } catch (UnsupportedEncodingException e) {
	            e.printStackTrace();
	        }
	    }
	 
	    public  String post(String url, Map<String, Object> params) throws IOException {
	        HttpPost httppost = new HttpPost(url);
	        config(httppost);
	        setPostParams(httppost, params);
	        CloseableHttpResponse response = null;
	        try {
	            response = getHttpClient(url).execute(httppost,HttpClientContext.create());
	            HttpEntity entity = response.getEntity();
	            String result = EntityUtils.toString(entity, "utf-8");
	            EntityUtils.consume(entity);
	            return result;
	        } catch (Exception e) {
//	          e.printStackTrace();
	            throw e;
	        } finally {
	            try {
	                if (response != null)
	                    response.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	    }
	 
	    public   String get(String url) {
	        HttpGet httpget = new HttpGet(url);
	        config(httpget);
	        CloseableHttpResponse response = null;
	        try {
	            response = getHttpClient(url).execute(httpget,HttpClientContext.create());
	            HttpEntity entity = response.getEntity();
	            String result = EntityUtils.toString(entity, "utf-8");
	            EntityUtils.consume(entity);   //关闭HttpEntity是的流，如果手动关闭了InputStream instream = entity.getContent();这个流，也可以不调用这个方法
	            return result;
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                if (response != null)
	                    response.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        return null;
	    }
	    
	    //用于监控空闲的连接池连接
	    private static final  class IdleConnectionMonitorThread extends Thread {
	        private   final HttpClientConnectionManager connMgr;
	        private  volatile boolean shutdown;

	        private static final int MONITOR_INTERVAL_MS = 5000;
	        private static final int IDLE_ALIVE_MS = 10;

	        public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
	             super();
	             this.connMgr = connMgr;
	             this.shutdown = false;
	        }

	        @Override
	        public void run() {
	            try {
	                while (!shutdown) {
	                    synchronized (this) {
	                        wait(MONITOR_INTERVAL_MS);
	                        // 关闭无效的连接
	                        connMgr.closeExpiredConnections();
	                        // 关闭空闲时间超过IDLE_ALIVE_MS的连接
	                        connMgr.closeIdleConnections(IDLE_ALIVE_MS, TimeUnit.MILLISECONDS);
	                }
	            }
	            } catch (InterruptedException e) {
	                e.printStackTrace();
	            }
	        }

	        // 关闭后台连接
	        public void shutdown() {
	            shutdown = true;
	            synchronized (this) {
	                notifyAll();
	            }
	        }
	    }
	 
	}